package com.hdsx.validator.validate.config;

import java.util.List;

import org.dom4j.Document;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import com.hdsx.validator.util.resource.Resources;
import com.hdsx.validator.validate.handler.IHandler;
import com.hdsx.validator.validate.handler.IValidator;

public class ValidateListener implements ApplicationListener<ContextRefreshedEvent> {

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		// TODO Auto-generated method stub
		IHandler handler=event.getApplicationContext().getBean(IHandler.class);
		try {
			Document doc = XMLParser.getDocument(Resources.getResourceAsStream("DefaultValidate.xml"));
			List<String> alias=XMLParser.getAttributes(doc,"validators/item", "alias");
			for(String alia:alias)
			{
				List<String> classpaths=XMLParser.getAttributes(doc,"validators/item[@"+alia+"]","class");
				for(String classpath:classpaths)
				{
					IValidator validator=(IValidator) Class.forName(classpath).newInstance();
					handler.register(alia, validator);
				}
			
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	

}
